/** @jsxRuntime automatic */
/** @jsxImportSource ../xml-runtime */
import { create, fragment } from 'xmlbuilder2'
import { render } from '../xml-runtime/shared'
import { getFramePairs } from '../auto-cut-video-lib/Frame'

import { Sequence } from './Sequence'
import { File } from './File'
import { Links } from './Links'

function getSequence(pathObject, mediaInfo, sequenceFrameCount, clipItems) {
  if (mediaInfo.width) {
    if (mediaInfo.channelCount === 1) {
      return (
        <Sequence.VideoMono
          pathObject={pathObject}
          mediaInfo={mediaInfo}
          clipItems={clipItems}
          duration={sequenceFrameCount}
          // vlevin @ 2022-07-04 mono视频，创建2个一组的<link>
          renderLinks={(index) => <Links.Mono index={index}></Links.Mono>}
        ></Sequence.VideoMono>
      )
    } else {
      return (
        <Sequence.VideoStereo
          pathObject={pathObject}
          mediaInfo={mediaInfo}
          clipItems={clipItems}
          duration={sequenceFrameCount}
          // vlevin @ 2022-07-04 stereo视频，创建3个一组的<link>
          renderLinks={(index) => <Links.Stereo index={index}></Links.Stereo>}
        ></Sequence.VideoStereo>
      )
    }
  } else {
    if (mediaInfo.channelCount === 1) {
      return (
        <Sequence.AudioMono
          pathObject={pathObject}
          mediaInfo={mediaInfo}
          clipItems={clipItems}
          duration={sequenceFrameCount}
        ></Sequence.AudioMono>
      )
    } else {
      return (
        <Sequence.AudioStereo
          pathObject={pathObject}
          mediaInfo={mediaInfo}
          clipItems={clipItems}
          duration={sequenceFrameCount}
        ></Sequence.AudioStereo>
      )
    }
  }
}

export function generateXML(encodedPathObject, mediaInfo, secPairs) {
  // vlevin @ 2022-06-29 计算剪辑点，以frame表示，顺便计算sequence总时长
  const framePairs = getFramePairs(secPairs, mediaInfo.frameRate)
  const clipItems = []
  let totalStart = 0
  for (const framePair of framePairs) {
    const clipLength = framePair[1] - framePair[0]
    clipItems.push({
      totalStart,
      totalEnd: totalStart + clipLength,
      start: framePair[0],
      end: framePair[1],
    })
    totalStart += clipLength
  }
  const sequenceFrameCount = totalStart

  // vlevin @ 2022-07-03 文件信息转回UTF-8
  const pathObject = Object.assign({}, encodedPathObject, {
    base: decodeURI(encodedPathObject.base),
    name: decodeURI(encodedPathObject.name),
  })

  // vlevin @ 2022-06-29 生成xml字符串
  const container = create({ encoding: 'UTF-8' }).dtd({ name: 'xmeml' })
  render(
    <xmeml version="4">
      {getSequence(pathObject, mediaInfo, sequenceFrameCount, clipItems)}
    </xmeml>,
    container
  )
  let xmlString = container.end()

  // vlevin @ 2022-07-03 生成源文件说明XML字符串，替换首个<clipitem>中的<file>
  const fileNode = fragment()
  render(
    mediaInfo.width ? (
      <File.Video pathObject={pathObject} mediaInfo={mediaInfo}></File.Video>
    ) : (
      <File.Audio pathObject={pathObject} mediaInfo={mediaInfo}></File.Audio>
    ),
    fileNode
  )
  const fileString = fileNode.end()
  xmlString = xmlString.replace('<file id="s"/>', fileString)

  return xmlString
}
